﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Data = {Data}" )]
  internal class IndexDataItem : RawDataItem
  {
    #region Static Fields

    // This property name is used to identify the index of an item.
    // It contains invalid property name symbols to make sure it
    // won't collid with an existing property name on the user's
    // data object.
    internal const string IndexKeyName = "#@@_INDEX_KEY_@@#";

    #endregion

    #region Constructor

    internal IndexDataItem( RawDataItem dataItem, IndexKey index )
      : base( dataItem.Data )
    {
      if( index == null )
        throw new ArgumentNullException( "index" );

      this.ChildDataItem = dataItem;
      this.Index = index;
    }

    #endregion

    #region ChildDataItem Property

    internal RawDataItem ChildDataItem
    {
      get;
      private set;
    }

    #endregion

    #region Index Property

    internal IndexKey Index
    {
      get;
      private set;
    }

    #endregion

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    public override bool Equals( object obj )
    {
      if( object.ReferenceEquals( obj, null ) )
        return false;

      if( object.ReferenceEquals( obj, this ) )
        return true;

      if( !( obj is IndexDataItem ) )
        return false;

      return base.Equals( obj );
    }

    protected override bool FindDataMemberValue( string memberName, out object value )
    {
      if( memberName == IndexDataItem.IndexKeyName )
      {
        value = this.Index;
        return true;
      }

      return base.FindDataMemberValue( memberName, out value );
    }

    internal override void Delete()
    {
      if( this.ChildDataItem != null )
      {
        this.ChildDataItem.Delete();
      }
    }
  }
}
